package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.CollectionBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.utils.ImageLoadUtils;

import java.util.List;

/**
 * @date on 2018/6/18 16:29
 * @email 909732184@qq.com
 * 我的收藏
 */
public class MyCollectionAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<CollectionBean> data;
    private OnItemClickListener onItemClickListener;

    public List<CollectionBean> getData() {
        return data;
    }

    public void setData(List<CollectionBean> data) {
        this.data = data;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onExaminationItemClick(int position);
    }
    public MyCollectionAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.my_collection_adapter_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = null;
        if(holder instanceof ViewHolder){
            viewHolder = (ViewHolder) holder;
            viewHolder.position = position;
            CollectionBean collectionBean = data.get(position);
            CoursesBean coursesBean = collectionBean.getCourse();
            if(coursesBean!=null){
                ImageLoadUtils.ImageLoad(context,coursesBean.getFaceUrl(),viewHolder.ivIcon,-1);
                viewHolder.tvTitle.setText(coursesBean.getName()+"");
                viewHolder.tvContent.setText(Html.fromHtml(coursesBean.getIntroduction()+""));
            }
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        private int position;
        private TextView tvGoExamination;
        private TextView tvTitle;
        private TextView tvContent;
        private ImageView ivIcon;
        public ViewHolder(View itemView) {
            super(itemView);
            tvGoExamination = itemView.findViewById(R.id.tvGoExamination);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvContent = itemView.findViewById(R.id.tvContent);
            ivIcon = itemView.findViewById(R.id.ivIcon);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onExaminationItemClick(position);
                    }
                }
            });
        }
    }
}
